package com.scm.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

public class Const {
	
	public static final char[] CHARS = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
			'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
			'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
			'2', '3', '4', '5', '6', '7', '8', '9' };
	
	/**
	 * 拆分字符串的字符
	 */
	public static final String SPLIT=",|，| |、";
	
	/**
	 * 邮箱的正则表达式
	 */
	public static final String EMAIL="^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)$";
	
	/**
	 * 电话和手机号码的正在表达式
	 */
	public static final String CONTACT="^(0\\d{2,3}-?\\d{7,8})|(1\\d{10})$";
	
	/**
	 * 时间格式
	 */
	public static final String TIME_FORMAT="yyyy-MM-dd HH:mm";
	
	/**
	 * 日期字符串
	 */
	public static String DATE_CODE;
	
	static{
		DATE_CODE=new SimpleDateFormat("yyMMdd").format(new Date());
	}
	
	/**
	 * 生成14位编码
	 * @return
	 */
	public static String getCode(){
		SimpleDateFormat smf=new SimpleDateFormat("yyyyMMdd");
		String code=smf.format(new Date());
		int[] array = {0,1,2,3,4,5,6,7,8,9};
		Random rand = new Random();
		for (int i = 10; i > 1; i--) {
		    int index = rand.nextInt(i);
		    int tmp = array[index];
		    array[index] = array[i - 1];
		    array[i - 1] = tmp;
		}
		int result = 0;
		for(int i = 0; i < 6; i++)
		    result = result * 10 + array[i];
		return code+result;
	}
	
	
	
	/**
	 * 样衣编码  yyMMdd 0001 111 中间4位
	 */
	public static int PROOFING_CODE=1;
	
	/**
	 * 生成样衣的编码
	 * @return
	 */
	public static synchronized String getSampleCode(){
		String str="0000"+PROOFING_CODE;
		PROOFING_CODE++;
		String code=DATE_CODE+str.substring(str.length()-4, str.length());
		Random rand = new Random();
		for (int i = 0; i < 3; i++) {
		    int index = rand.nextInt(10);
		    code+=index;
		}
		return code;
	}
	
	/**
	 * 客户编码  yyMMdd 001 *** 中间3位自增
	 */
	public static int CUSTOMER_CODE=1;
	
	/**
	 * 产生编码
	 * @param no
	 * @return
	 */
	public static synchronized String getCustomerCode(){
		return getCode(CUSTOMER_CODE++);
	}
	
	/**
	 * 工厂编码  yyMMdd 001 *** 中间3位自增
	 */
	public static int FACTORY_CODE=1;
	
	/**
	 * 获得工厂的编码
	 * @return
	 */
	public static synchronized String getFactoryCode(){
		return getCode(FACTORY_CODE++);
	}
	
	/**
	 * 订单编码  yyMMdd 001 **** 中间3位自增
	 */
	public static int ORDER_CODE=1;
	
	/**
	 * 获得订单的编号
	 * @return
	 */
	public static synchronized String getOrderCode(){
		return getCode(ORDER_CODE++)+(int)(Math.random()*10);
	}
	
	/**
	 * 产生编码
	 * @param no
	 * @return
	 */
	public static String getCode(int no){
		String str="00"+no;
		String code=DATE_CODE+str.substring(str.length()-3, str.length());
		Random rand = new Random();
		for (int i = 0; i < 3; i++) {
		    int index = rand.nextInt(10);
		    code+=index;
		}
		return code;
	}
	
	/**
	 * 子订单编码  yyMMdd     001           001       **** 
	 *                   母订单的编号         中间3位自增
	 */
	public static int SUB_ORDER_CODE=1;
	
	/**
	 * 获得子订单的编码
	 * @param orderCode 母订单的编码
	 * @return
	 */
	public static synchronized String getSubOrderCode(String orderCode){
		String str="00"+SUB_ORDER_CODE;
		SUB_ORDER_CODE++;
		String code=orderCode.substring(0,orderCode.length()-4)+str.substring(str.length()-3, str.length());
		Random rand = new Random();
		for (int i = 0; i < 4; i++) {
		    int index = rand.nextInt(10);
		    code+=index;
		}
		return code;
	}
	
	/**
	 * 产生第 no 款子订单的编码
	 * @param orderCode
	 * @param no
	 * @return
	 */
	public static String getSubOrderCode(String orderCode,int no){
		String str="00"+no;
		String code=orderCode.substring(0,orderCode.length()-4)+str.substring(str.length()-3, str.length());
		Random rand = new Random();
		for (int i = 0; i < 4; i++) {
		    int index = rand.nextInt(10);
		    code+=index;
		}
		return code;
	}
	
	public static void resetSubOrderCode(){
		SUB_ORDER_CODE=1;
	}
	
	/**
	 * 获取路径，有参数时，带上参数
	 * @param req
	 * @return
	 */
	public static String getPath(HttpServletRequest req){
		String path=req.getServletPath();
		path=path.substring(path.lastIndexOf("/")+1);
		String queryString=req.getQueryString();
		if(queryString==null){
			return path+"?";
		}
		if(queryString.indexOf("pageNo")!=-1){
			queryString=queryString.substring(0, queryString.lastIndexOf("pageNo"));
		}
		if(queryString==null||queryString.trim().isEmpty()){
			return path+"?";
		}
		if(!queryString.endsWith("&")) queryString=queryString+"&";
		return path+"?"+queryString;
	}
	
	/**
	 * 获取6位随机密码（3位英文字母，3位数字）
	 * @return
	 */
	public static String getRandomPassword(){
		char password[] = new char[6];
		for(int i=0;i<6;i++){
			int index = (int)(Math.random()*62);
			password[i]=CHARS[index];
		}
		return String.valueOf(password);
	}
	
	/**
	 * 款式细节明细
	 */
	public static final String DETAIL_DESCRIPTION="<table border='1px solid #ccc' cellpadding='1' cellspacing='0' class='particulars center' style='height:auto; width:96%; margin-top: 10px'>"
			+ "<tbody><tr><th style='background-color:#f6f6f6; height:35px; text-align:center; width:20%'>细节</th><th style='background-color:#f6f6f6; text-align:center; width:20%'>描述</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:20%'>尺寸大小</th><th style='background-color:#f6f6f6; text-align:center; width:20%'>位置</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:20%'>备注</th></tr><tr><td style='height:35px; text-align:center'></td>"
			+ "<td style='text-align:center'></td><td style='text-align:center'></td><td style='text-align:center'></td><td style='text-align:center'></td></tr><tr>"
			+ "<td style='height:35px; text-align:center'></td><td style='text-align:center'></td><td style='text-align:center'></td><td style='text-align:center'></td>"
			+ "<td style='text-align:center'></td></tr><tr><td style='height:35px; text-align:center'></td><td style='text-align:center'></td><td style='text-align:center'></td>"
			+ "<td style='text-align:center'></td><td style='text-align:center'></td></tr></tbody></table>";
	
	/**
	 * 面辅料明细
	 */
	public static final String ACCESSORT_DETAIL="<table border='1px solid #ccc' cellpadding='1' cellspacing='0' class='particulars center' style='height:auto; width:96%; margin-top: 10px'>"
			+ "<tbody><tr><th colspan='7' style='background-color:#f6f6f6; height:35px; text-align:center'>面料明细</th></tr><tr>"
			+ "<th style='background-color:#f6f6f6; height:35px; text-align:center; width:12%'>&nbsp;</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:11%'>颜色</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:13%'>面料名称</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:12%'>成分</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:16%'>供应商信息</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:16%'>供应商编号</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:20%'>所用部位</th></tr>"
			+ "<tr><td style='height:35px; text-align:center'></td><td style='text-align:center'>&nbsp;</td>"
			+ "<td style='text-align:center'></td><td style='text-align:center'>&nbsp;</td><td style='text-align:center'>&nbsp;</td>"
			+ "<td style='text-align:center'>&nbsp;</td><td style='text-align:center'>&nbsp;</td></tr><tr>"
			+ "<td style='height:35px; text-align:center'></td><td style='text-align:center'>&nbsp;</td>"
			+ "<td style='text-align:center'>&nbsp;</td><td style='text-align:center'>&nbsp;</td><td style='text-align:center'>&nbsp;</td>"
			+ "<td style='text-align:center'>&nbsp;</td><td style='text-align:center'>&nbsp;</td></tr><tr>"
			+ "<td style='height:35px; text-align:center'></td><td style='text-align:center'>&nbsp;</td>"
			+ "<td style='text-align:center'>&nbsp;</td><td style='text-align:center'>&nbsp;</td><td style='text-align:center'>&nbsp;</td>"
			+ "<td style='text-align:center'>&nbsp;</td><td style='text-align:center'>&nbsp;</td></tr></tbody></table>"
			+ ""
			+ "<table border='1px solid #ccc' cellpadding='1' cellspacing='0' class='particulars center' style='height:auto; width:96%; margin-top: 10px'>"
			+ "<tbody><tr><th colspan='6' style='background-color:#f6f6f6; height:35px; text-align:center'>辅料明细</th></tr><tr>"
			+ "<th style='background-color:#f6f6f6; height:35px; text-align:center; width:15%'>辅料名称</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:15%'>数量</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:15%'>位置</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:15%'>车法细节</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:20%'>明细图片</th>"
			+ "<th style='background-color:#f6f6f6; text-align:center; width:20%'>备注</th></tr><tr>"
			+ "<td style='height:35px; text-align:center; width:15%'>&nbsp;</td>"
			+ "<td style='text-align:center; width:15%'>&nbsp;</td><td style='text-align:center; width:15%'>&nbsp;</td>"
			+ "<td style='text-align:center; width:15%'>&nbsp;</td><td style='text-align:center; width:20%'>&nbsp;</td>"
			+ "<td style='text-align:center; width:20%'>&nbsp;</td></tr></tbody></table>";
}
